/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.datasets.mq;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.medusa.gruul.datasets.api.constant.DataSetsQueueNameConstant;
import com.medusa.gruul.datasets.api.model.DataSetsAddMessage;
import com.medusa.gruul.datasets.service.IDcShopActiveAnalyseService;
import com.medusa.gruul.datasets.service.IDcShopRankingService;
import com.medusa.gruul.order.api.model.OrderVo;
import lombok.extern.slf4j.Slf4j;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @Description: 数据中心队列监听
 * @Author: xiaoq
 * @Date : 2020/05/06 14:40
 */
@Slf4j
@Component
public class DataSetsListener {

	private final IDcShopRankingService dcShopRankingService;

	private final IDcShopActiveAnalyseService dcShopActiveAnalyseService;

	public DataSetsListener(IDcShopRankingService dcShopRankingService,
			IDcShopActiveAnalyseService dcShopActiveAnalyseService) {
		this.dcShopRankingService = dcShopRankingService;
		this.dcShopActiveAnalyseService = dcShopActiveAnalyseService;
	}
	//===============================订阅======================================//

	/**
	 * 监听订单状态 并插入数据  状态 :订单支付成功
	 *
	 * @param orderVo 订单信息
	 * @param properties 消息
	 * @param channel 通道
	 * @throws IOException
	 */
	@RabbitListener(queues = DataSetsQueueNameConstant.ORDER_PAY_OK)
	public void receiveSendPayedOrderMessage(OrderVo orderVo, MessageProperties properties, Channel channel)
			throws IOException {
		log.info("receive message:" + orderVo.toString());
		if (ObjectUtil.isNull(orderVo)) {
			//手动确认
			channel.basicAck(properties.getDeliveryTag(), true);
			return;
		}
		dcShopRankingService.orderPayFinish(orderVo);
		//手动确认
		channel.basicAck(properties.getDeliveryTag(), true);
	}




	@RabbitListener(queues = DataSetsQueueNameConstant.NEW_USER_VISIT)
	public void uvAdd(DataSetsAddMessage dataSetsAddMessage, MessageProperties properties, Channel channel) {
		log.info("收到新用户访问消息:当前时间{},消息内容{}", DateUtil.now(), dataSetsAddMessage.toString());
		System.out.println("收到新用户访问消息:当前时间{},消息内容{}" + DateUtil.now() + dataSetsAddMessage);
		try {
			String shopId = dataSetsAddMessage.getShopId();
			String tenantId = dataSetsAddMessage.getTenantId();
			dcShopActiveAnalyseService.saveCallerInfo(shopId, tenantId);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}

}
